package lingou.homework.postbar.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.servlet.http.HttpSession;
import lingou.homework.postbar.controller.proto.resp.Response;
import lingou.homework.postbar.controller.proto.UserView;
import lingou.homework.postbar.controller.tool.FormatHelper;
import lingou.homework.postbar.controller.tool.SessionHelper;
import lingou.homework.postbar.domain.User;
import lingou.homework.postbar.exception.BusinessException;
import lingou.homework.postbar.service.UserService;
import net.sf.jsqlparser.expression.TimestampValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Api(tags = "用户信息")
@RestController
@RequestMapping("/user/info")
public class UserInfoController {
    @Autowired
    UserService userService;
    @Autowired
    UserView userView;

    @ApiOperation("获取用户信息")
    @GetMapping
    Response getInfo(@ApiParam("用户ID") Long userId, HttpSession session) {
        if (!SessionHelper.isLogin(session)) return SessionHelper.NOT_LOGGED;
        if (userId == null) userId = SessionHelper.getCurrentUser(session);
        User user;
        if(userId == -1){
            user = new User();
            user.setUserType(-1L);
            user.setUserName("匿名用户");
            user.setEmail("");
            user.setStatus(0L);
            user.setTime(new TimestampValue("2000-01-01 00:00:00").getValue());
        }
        else {
            user = userService.getUser(userId);
            if (user == null) return Response.ofFailure("该用户不存在！");
        }
        return Response.ofSuccess(userView.attachInfo(user));
    }

    @PutMapping
    Response modifyInfo(String userName, String email, HttpSession session) {
        Long id = SessionHelper.getCurrentUser(session);
        if (id == null) return SessionHelper.NOT_LOGGED;
        User user = userService.getUser(id);
        if (userName != null) {
            Response fail = FormatHelper.illegalUsername(userName);
            if (fail != null) return fail;
            user.setUserName(userName);
        }
        if (email != null) {
            Response fail = FormatHelper.illegalEmail(email, userService::exists);
            if (fail != null) return fail;
            user.setEmail(email);
        }
        userService.updateUser(user);
        return Response.ofSuccess();
    }

    @PutMapping("/pwd")
    Response modifyPassword(String pwd, String oldPwd, HttpSession session) {
        Long id = SessionHelper.getCurrentUser(session);
        if (id == null) return SessionHelper.NOT_LOGGED;
        User user = userService.getUser(id);
        if (!user.getPassword().equals(oldPwd)) return Response.ofFailure("密码错误！");
        Response fail = FormatHelper.illegalPassword(pwd);
        if (fail != null) return fail;
        user.setPassword(pwd);
        userService.updateUser(user);
        return Response.ofSuccess();
    }

    @PutMapping("/identify")
    Response identify(String studentId, String name, MultipartFile image, HttpSession session) throws IOException {
        Long id = SessionHelper.getCurrentUser(session);
        if (id == null) return SessionHelper.NOT_LOGGED;
        Response fail = FormatHelper.illegalIdentity(name, studentId);
        if (fail != null) return fail;
        userService.identifyRequest(id, name, studentId, image);
        return Response.ofSuccess();
    }

    @GetMapping("/identify/manage")
    Response getIdentifyRequest(HttpSession session){
        long userId = SessionHelper.checkLogin(session);
        if (!userService.isManager(userId)) throw BusinessException.NO_PERMISSION;
        return Response.ofSuccess(userService.getUnidentified());
    }

    @PutMapping ("/identify/manage")
    Response identifyPass(Long userId, HttpSession session){
        long current = SessionHelper.checkLogin(session);
        userService.identifyPass(current, userId);
        return Response.ofSuccess();
    }

}
